/*
*  Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
*  WSO2 Inc. licenses this file to you under the Apache License,
*  Version 2.0 (the "License"); you may not use this file except
*  in compliance with the License.
*  You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package com.jisc.esb.tasks;

import org.apache.axiom.om.OMElement;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class HealthCheckWorker implements Runnable {

    private static final Log log = LogFactory.getLog(HealthCheckWorker.class);

    private String endpoint;
    private String action;
    private OMElement message;
    private ConfigurationContext configCtx;

    private boolean success;
    private OMElement response;
    private Exception exception;

    public HealthCheckWorker(String endpoint, String action,
                             OMElement message, ConfigurationContext configCtx) {
        this.endpoint = endpoint;
        this.action = action;
        this.message = message;
        this.configCtx = configCtx;
    }

    public void run() {
        ServiceClient client;
        try {
            if (log.isDebugEnabled()) {
                log.debug("Creating new service client instance for endpoint: " + endpoint);
            }
            client = new ServiceClient(configCtx, null);
        } catch (AxisFault axisFault) {
            String msg = "Error while initializing service client for endpoint: " + endpoint;
            log.error(msg, axisFault);
            return;
        }

        Options options = new Options();
        options.setTo(new EndpointReference(endpoint));
        if (action != null) {
            options.setAction(action);
        }

        client.setOptions(options);
        try {
            if (log.isDebugEnabled()) {
                log.debug("Sending heartbeat to: " + endpoint);
            }
            response = client.sendReceive(message);
            success = true;
        } catch (Exception ex) {
            exception = ex;
            success = false;
        } finally {
            if (log.isDebugEnabled()) {
                log.debug("Cleaning up service client instance for endpoint: " + endpoint);
            }

            try {
                client.cleanupTransport();
            } catch (AxisFault axisFault) {
                log.warn("Error cleaning up transports", axisFault);
            }
            try {
                client.cleanup();
            } catch (AxisFault axisFault) {
                log.warn("Error cleaning up service client", axisFault);
            }
        }
    }

    public String getEndpoint() {
        return endpoint;
    }

    public boolean isSuccess() {
        return success;
    }

    public OMElement getResponse() {
        return response;
    }

    public Exception getException() {
        return exception;
    }
}
